//
//  LotteryCollectViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/20.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "LotteryCollectViewController.h"
#import "MineViewModel.h"
#import "WinRecordViewCell.h"

@interface LotteryCollectViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>


@property (nonatomic,strong) MineViewModel * viewModel;
@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) UIView * tempView;
@end

@implementation LotteryCollectViewController

-(MineViewModel *) viewModel{
    if (!_viewModel) {
        _viewModel = [[MineViewModel alloc] init];
    }
    return _viewModel;
}

-(UITableView *) tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"彩票收藏"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    
    
    [self createTempView];
    
    
   self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
       [self netWork];
   }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreTable];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
}

-(void) netWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    [[self.viewModel collectionInfoRequestParameter:LoadData] subscribeNext:^(id x) {
        
        if (self.viewModel.collectionInfoArrayList.count == 0) {
            
            [self.tempView setHidden:NO];
        }
        else{
            [self.tempView setHidden:YES];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [MXUtility hideMBProgress:self.view];
        
    }error:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [MXUtility hideMBProgress:self.view];
    }];
}

-(void) loadMoreTable {
    
    [[self.viewModel collectionInfoRequestParameter:LoadMore] subscribeNext:^(id x) {
      
        if (self.viewModel.collectionInfoArrayList.count == 0) {
            
            [self.tempView setHidden:NO];
        }
        else{
            [self.tempView setHidden:YES];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [MXUtility hideMBProgress:self.view];
        
    }error:^(NSError *error) {
        
        [self.tableView.mj_footer endRefreshing];
        [MXUtility hideMBProgress:self.view];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.viewModel.collectionInfoArrayList count];;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str = @"cell";
    WinRecordViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"WinRecordViewCell" owner:self options:nil] lastObject];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell collDataSource:[self.viewModel.collectionInfoArrayList objectAtIndex:indexPath.row]];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


-(void)createTempView{
    
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.tableView.width, self.tableView.height)];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(DEF_DEVICE_WIDTH/2-25, 200, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,260, DEF_DEVICE_WIDTH, 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    label.text = @"你还没有收藏过彩票";
    [self.tempView addSubview:label];
    
    [self.tableView addSubview:self.tempView];
    self.tempView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
